﻿using System;
using System.Collections.Generic;
using System.Linq;
using NReader.Model.RAS;
using NReader.Service;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;

// “项目页”项模板在 http://go.microsoft.com/fwlink/?LinkId=234233 上提供

namespace XnReader.WinRT
{
    /// <summary>
    /// 显示项预览集合的页。在“拆分布局应用程序”中，此页
    /// 用于显示及选择可用组之一。
    /// </summary>
    public sealed partial class BookStore
    {
        public BookStore()
        {
            InitializeComponent();
            var toolBar = new ToolBarInfo
            {
                VisibleAddStore = Visibility.Collapsed,
                VisibleBookReader = Visibility.Collapsed,
                VisibleCommend = Visibility.Collapsed,
                VisibleContactUs = Visibility.Collapsed,
                VisibleDelStore = Visibility.Visible,
                VisibleLogin = Visibility.Collapsed,
                VisibleRights = Visibility.Collapsed,
                VisibleSearch = Visibility.Visible,
                VisibleSetting = Visibility.Collapsed
            };
            tlBottom.DataContext = toolBar;
            tlBottom.DelStoreClickEvent += TlBottomDelStore;
        }

        async void TlBottomDelStore(object sender, RoutedEventArgs e)
        {
            IList<object> selectedItems = itemGridView.Visibility==Visibility.Visible ? itemGridView.SelectedItems : itemListView.SelectedItems;

            if (selectedItems != null && selectedItems.Count > 0)
            {
                var items = selectedItems.Aggregate("(", (current, selectedItem) =>
                {
                    var articleInfo = selectedItem as ArticleInfo;
                    return articleInfo != null
                               ? current +
                                 string.Format("{0},",
                                               articleInfo.articleid)
                               : null;
                });
                items = items.Remove(items.Length - 1, 1);
                items += ")";
                if (await ArticleService.Instance().DeleteBookStore(items))
                {
                    #region 修改为遍历删除，界面未生效

                    //var lists = this.DefaultViewModel["ArticleList"] as IList<ArticleInfo>;
                    //if (lists != null)
                    //{
                    //    for (int i = selectedItems.Count - 1; i >= 0; i--)
                    //    {
                    //        lists.Remove(selectedItems[i] as ArticleInfo);
                    //    }
                    //}

                    #endregion

                    DataBinding();
                    BottomAppBar.IsOpen = false;
                }
            }
        }

        /// <summary>
        /// 使用在导航过程中传递的内容填充页。在从以前的会话
        /// 重新创建页时，也会提供任何已保存状态。
        /// </summary>
        /// <param name="navigationParameter">最初请求此页时传递给
        /// <see cref="Frame.Navigate(Type, Object)"/> 的参数值。
        /// </param>
        /// <param name="pageState">此页在以前会话期间保留的状态
        /// 字典。首次访问页面时为 null。</param>
        protected override void LoadState(Object navigationParameter, Dictionary<String, Object> pageState)
        {
            DataBinding();
        }

        private async void DataBinding()
        {
            var list = await ArticleService.Instance().GetBookStoreLocal(1);
            DefaultViewModel["ArticleList"] = list;
            cvsArticleList.Source = list;
            txtCount.Text = string.Format("{0}/30", list.Count);
        }

        private void ItemGridViewItemClick(object sender, ItemClickEventArgs e)
        {
            if (e.ClickedItem != null)
            {
                int articleid = ((ArticleInfo)e.ClickedItem).articleid;
                Frame.Navigate(typeof(Book), articleid);
            }
        }

        private void ItemGridViewSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (itemGridView.Visibility == Visibility.Visible)
                BottomAppBar.IsOpen = itemGridView.SelectedItems.Count > 0;
            else
            {
                BottomAppBar.IsOpen = itemListView.SelectedItems.Count > 0;
            }
        }
    }
}
